#include<bits/stdc++.h>
using namespace std;

unordered_map<string, int> st;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
char a[10];
string s;
int d;
string ed = "12345678x";
void bfs(){
    queue<string> q;
    q.push(s);
    st[s] = 0;

    while(!q.empty()){
        string t = q.front();
        if (t == ed) return ;
        if(st[s] > 50) return;
        q.pop();
        int loc = t.find("x");
        int x = loc / 3;
        int y = loc % 3;

        for (int i = 0; i < 4; i++){
            string tm = t;
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (xx < 0 || xx >= 3 || yy < 0 || yy >= 3) continue;

            swap(tm[loc], tm[xx * 3 + yy]);
            if (st.find(tm) != st.end()) continue;

            st[tm] = st[t] + 1;
            if (st[tm] == 30) cout << tm << endl;
            q.push(tm);
        }
    }
}   
int main(){
    for (int i = 1; i <= 9; i++){
        cin >> a[i];
        s += a[i];
    }

    // 0 1 2
    // 3/3 3 % 3 4 % 3 5 % 3
    // 
    bfs();
    if (st.find(ed) != st.end()) cout << st[ed];
    else cout << - 1 << endl;
    return 0;
}